package repositories

import (
    "context"

    "github.com/samaa/photosalesplus/backend/internal/models"
    "gorm.io/gorm"
)

type MerchantRepository interface {
    Create(ctx context.Context, m *models.Merchant) error
    FindByUsername(ctx context.Context, username string) (*models.Merchant, error)
    FindByID(ctx context.Context, id uint) (*models.Merchant, error)
}

type merchantRepository struct{ db *gorm.DB }

func NewMerchantRepository(db *gorm.DB) MerchantRepository { return &merchantRepository{db: db} }

func (r *merchantRepository) Create(ctx context.Context, m *models.Merchant) error {
    return r.db.WithContext(ctx).Create(m).Error
}

func (r *merchantRepository) FindByUsername(ctx context.Context, username string) (*models.Merchant, error) {
    var m models.Merchant
    if err := r.db.WithContext(ctx).Where("username = ?", username).First(&m).Error; err != nil {
        return nil, err
    }
    return &m, nil
}

func (r *merchantRepository) FindByID(ctx context.Context, id uint) (*models.Merchant, error) {
    var m models.Merchant
    if err := r.db.WithContext(ctx).First(&m, id).Error; err != nil {
        return nil, err
    }
    return &m, nil
}

